﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP
{
    /// <summary>來電顯示訊息Push</summary>
    public class call_push : IHttpHandler
    {
        private static string[] kAdminEmailList =
    { "harriet@ccp.com.tw", "david_lo@ccp.com.tw", "joe_zeng@ccp.com.tw",
      "ch_chou@ccp.com.tw", "hj_huang@ccp.com.tw",
      "mike_lin@ccp.com.tw", "chuck_lan@ccp.com.tw"
    };
        private const string kReportEmailAddr = "ccpsip_report@ccp.com.tw";

        private const string kValidIPAddr = "118.163.125.205";
        private const string kValidAppID = "tw.com.ccp.ccpsip";


        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";

            try
            {
                //【01】檢查呼叫來源IP
                //
                string serverIP = ChrisKit.getIPAddress(req);
                if (serverIP != kValidIPAddr)
                {
                    throw new Exception("Error_01: Access Denied");//禁止存取
                }


                //【02】取得GET參數
                //
                string appID = req.QueryString["appID"].ToLower();//App ID(檢查用)
                if (appID != kValidAppID)
                {
                    throw new Exception("Error_02: Invalid App ID");//AppID不正確
                }

                string caller = req.QueryString["caller"];//發話號碼
                string callee = req.QueryString["callee"];//受話號碼
                bool isVideo = false;
                if (req.QueryString["video"] == "1")
                {
                    //1=視訊,0=語音來電
                    isVideo = true;
                }


                isVideo = false;//Debug: 尚未區分語音/視訊來電


                //【03】儲存待推播訊息
                //
                OracleKit.InsertCallPush(callee, caller, isVideo);


                //【04】通知Push Service
                //
                PushKit.WakePushAsync(PushKit.PushType.Call);


                resultStr = "OK";
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);

                //【Admin】寄送錯誤通知Email
                //
                Dictionary<string, string> paramDict = ChrisKit.ToDict(req.QueryString);
                paramDict["ServerIP"] = ChrisKit.getIPAddress(req);
                string errorMsg = ChrisKit.Ex2JSON(ex, true)
                  + Environment.NewLine + ChrisKit.DictToJSON(paramDict, true);
                ChrisKit.SendCCPEmail(kReportEmailAddr, kAdminEmailList, null, "【通報】iSender Call Push 失敗", errorMsg);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
